<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>自有库存管理 - 虚拟仓系统</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <!-- 统一导航栏 -->
    {% include 'includes/navbar.html' %}

    <div class="container mt-4">
        <div class="row mb-4">
            <div class="col-md-6">
                <div class="input-group">
                    <input type="text" id="searchName" class="form-control" placeholder="搜索商品名称">
                    <select id="filterWarehouse" class="form-select">
                        <option value="">所有仓</option>
                        <option value="华东仓">华东仓</option>
                        <option value="华南仓">华南仓</option>
                    </select>
                    <button id="searchBtn" class="btn btn-primary">搜索</button>
                </div>
            </div>
            <div class="col-md-6 text-end">
                <button class="btn btn-success" data-bs-toggle="modal" data-bs-target="#inModal">入库</button>
                <button class="btn btn-warning" data-bs-toggle="modal" data-bs-target="#outModal">出库</button>
            </div>
        </div>
        <div class="card">
            <div class="card-body">
                <table class="table table-striped">
                    <thead>
                        <tr>
                            <th>商品ID</th>
                            <th>名称</th>
                            <th>数量</th>
                            <th>仓库</th>
                            <th>类型</th>
                            <th>更新时间</th>
                        </tr>
                    </thead>
                    <tbody id="inventoryTable"></tbody>
                </table>
            </div>
        </div>
    </div>

    <!-- 入库弹窗 -->
    <div class="modal fade" id="inModal" tabindex="-1">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header"><h5 class="modal-title">商品入库</h5><button class="btn-close" data-bs-dismiss="modal"></button></div>
                <div class="modal-body">
                    <form id="inForm">
                        {{ csrf_token }}
                        <div class="mb-3"><label>商品ID</label><input type="text" class="form-control" name="product_id" required></div>
                        <div class="mb-3"><label>商品名称</label><input type="text" class="form-control" name="product_name" required></div>
                        <div class="mb-3"><label>数量</label><input type="number" class="form-control" name="quantity" min="1" required></div>
                        <div class="mb-3"><label>仓库</label>
                            <select class="form-select" name="warehouse" required>
                                <option value="华东仓">华东仓</option>
                                <option value="华南仓">华南仓</option>
                            </select>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                    <button id="submitIn" class="btn btn-success">确认入库</button>
                </div>
            </div>
        </div>
    </div>

    <!-- 出库弹窗 -->
    <div class="modal fade" id="outModal" tabindex="-1">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header"><h5 class="modal-title">商品出库</h5><button class="btn-close" data-bs-dismiss="modal"></button></div>
                <div class="modal-body">
                    <form id="outForm">
                        {{ csrf_token }}
                        <div class="mb-3"><label>商品ID</label><input type="text" class="form-control" name="product_id" required></div>
                        <div class="mb-3"><label>数量</label><input type="number" class="form-control" name="quantity" min="1" required></div>
                        <div class="mb-3"><label>仓库</label>
                            <select class="form-select" name="warehouse" required>
                                <option value="华东仓">华东仓</option>
                                <option value="华南仓">华南仓</option>
                            </select>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                    <button id="submitOut" class="btn btn-warning">确认出库</button>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        // 加载库存列表（带/api前缀）
        const loadInventory = async () => {
            const warehouse = document.getElementById('filterWarehouse').value;
            const productName = document.getElementById('searchName').value;
            const res = await fetch(`/api/inventory?warehouse=${encodeURIComponent(warehouse)}&product_name=${encodeURIComponent(productName)}`);
            const data = await res.json();
            const table = document.getElementById('inventoryTable');
            table.innerHTML = data.map(item => `
                <tr>
                    <td>${item.product_id}</td>
                    <td>${item.product_name}</td>
                    <td>${item.quantity}</td>
                    <td>${item.warehouse}</td>
                    <td>${item.inventory_type}</td>
                    <td>${item.update_time}</td>
                </tr>
            `).join('');
        };

        // 入库操作（带/api前缀）
        document.getElementById('submitIn').addEventListener('click', async () => {
            const formData = Object.fromEntries(new FormData(document.getElementById('inForm')));
            const res = await fetch('/api/inventory/in', {
                method: 'POST',
                headers: {'Content-Type': 'application/json'},
                body: JSON.stringify(formData)
            });
            const data = await res.json();
            alert(data.message + (data.doc_id ? `\n单据编号：${data.doc_id}` : ''));
            if (data.status === 'success') {
                document.getElementById('inForm').reset();
                document.querySelector('#inModal .btn-close').click();
                loadInventory();
                setTimeout(() => window.location.href = '/stock/docs', 1000);
            }
        });

        // 出库操作（带/api前缀）
        document.getElementById('submitOut').addEventListener('click', async () => {
            const formData = Object.fromEntries(new FormData(document.getElementById('outForm')));
            const res = await fetch('/api/inventory/out', {
                method: 'POST',
                headers: {'Content-Type': 'application/json'},
                body: JSON.stringify(formData)
            });
            const data = await res.json();
            alert(data.message + (data.doc_id ? `\n单据编号：${data.doc_id}` : ''));
            if (data.status === 'success') {
                document.getElementById('outForm').reset();
                document.querySelector('#outModal .btn-close').click();
                loadInventory();
                setTimeout(() => window.location.href = '/stock/docs', 1000);
            }
        });

        document.getElementById('searchBtn').addEventListener('click', loadInventory);
        document.getElementById('logout').addEventListener('click', () => window.location.href = '/login_page');
        window.onload = loadInventory;
    </script>
</body>
</html>